/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.hpvas.gui.internalframe;

import edu.hpvas.control.OwnerController;
import edu.hpvas.control.PetController;
import edu.hpvas.control.Restrictions;
import edu.hpvas.persistence.dao.PetDAO;
import edu.hpvas.persistence.entity.Owner;
import edu.hpvas.persistence.entity.Pet;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Usuario
 */
public class PetUpdateInf extends javax.swing.JInternalFrame {

    /**
     * Creates new form PetUpdateInf
     */
    Restrictions restric = new Restrictions();
    Pet pet = new Pet();
    ArrayList<Pet> pets = new ArrayList<>();
    PetController petC= new PetController();
    public PetUpdateInf() {
        initComponents();
        initBlankComp();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nameJTF = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        nameJCB = new javax.swing.JComboBox();
        selecJB = new javax.swing.JButton();
        cancelJB = new javax.swing.JButton();
        errorJL = new javax.swing.JLabel();
        addPanelJP = new javax.swing.JPanel();
        nameJT = new javax.swing.JTextField();
        speciesJT = new javax.swing.JTextField();
        ageJT = new javax.swing.JTextField();
        weithJT = new javax.swing.JTextField();
        weigthJL = new javax.swing.JLabel();
        speciesJL = new javax.swing.JLabel();
        ageJL = new javax.swing.JLabel();
        nameJL = new javax.swing.JLabel();
        addJB = new javax.swing.JButton();
        caractersJL = new javax.swing.JLabel();
        imageF = new javax.swing.JLabel();

        jLabel1.setText("Search by name");

        nameJTF.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nameJTFMouseClicked(evt);
            }
        });

        jButton1.setText("Enter");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        selecJB.setText("Select");
        selecJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selecJBActionPerformed(evt);
            }
        });

        cancelJB.setText("Cancel");
        cancelJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJBActionPerformed(evt);
            }
        });

        errorJL.setForeground(new java.awt.Color(204, 0, 0));
        errorJL.setText("The special caracters are not allowed... try again");

        addPanelJP.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pet info", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(51, 0, 51))); // NOI18N

        nameJT.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nameJTMouseClicked(evt);
            }
        });

        speciesJT.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                speciesJTMouseClicked(evt);
            }
        });

        ageJT.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ageJTMouseClicked(evt);
            }
        });

        weithJT.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                weithJTMouseClicked(evt);
            }
        });

        weigthJL.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        weigthJL.setText("Weight");

        speciesJL.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        speciesJL.setText("Species");

        ageJL.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        ageJL.setText("Age");

        nameJL.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        nameJL.setText("Name");

        addJB.setText("Update");
        addJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addJBActionPerformed(evt);
            }
        });

        caractersJL.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        caractersJL.setForeground(new java.awt.Color(255, 0, 0));
        caractersJL.setText("The special caracters are not allowed... try again");

        javax.swing.GroupLayout addPanelJPLayout = new javax.swing.GroupLayout(addPanelJP);
        addPanelJP.setLayout(addPanelJPLayout);
        addPanelJPLayout.setHorizontalGroup(
            addPanelJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addPanelJPLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(addPanelJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(addPanelJPLayout.createSequentialGroup()
                        .addComponent(caractersJL, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(addJB, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(addPanelJPLayout.createSequentialGroup()
                        .addGroup(addPanelJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(addPanelJPLayout.createSequentialGroup()
                                .addComponent(weigthJL)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(weithJT, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(addPanelJPLayout.createSequentialGroup()
                                .addComponent(nameJL)
                                .addGap(18, 18, 18)
                                .addComponent(nameJT, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(addPanelJPLayout.createSequentialGroup()
                                .addGroup(addPanelJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(speciesJL)
                                    .addComponent(ageJL))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(addPanelJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ageJT, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(speciesJT, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(18, 18, 18)
                        .addComponent(imageF, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33))))
        );
        addPanelJPLayout.setVerticalGroup(
            addPanelJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addPanelJPLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(addPanelJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(addPanelJPLayout.createSequentialGroup()
                        .addGroup(addPanelJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nameJL)
                            .addComponent(nameJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(addPanelJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(speciesJL)
                            .addComponent(speciesJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(addPanelJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ageJL)
                            .addComponent(ageJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(addPanelJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(weigthJL)
                            .addComponent(weithJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(imageF, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(addPanelJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(addPanelJPLayout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(caractersJL)
                        .addContainerGap(57, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addPanelJPLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(addJB, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(nameJTF, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(50, 50, 50)
                                .addComponent(jButton1))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(nameJCB, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(selecJB, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelJB))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(errorJL, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(addPanelJP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nameJTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(8, 8, 8)
                .addComponent(errorJL)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameJCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selecJB)
                    .addComponent(cancelJB))
                .addGap(52, 52, 52)
                .addComponent(addPanelJP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nameJTFMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nameJTFMouseClicked

        if(nameJTF.getText().equals("The field Cannot be Blank")){
            nameJTF.setText("");
            nameJTF.setForeground(Color.black);
        }
    }//GEN-LAST:event_nameJTFMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        errorJL.setVisible(false);
        nameJCB.removeAllItems();
        if(nameJTF.getText().equals("") || nameJTF.getText().equals("The field Cannot be Blank")){
            nameJTF.setText("The field Cannot be Blank");
            nameJTF.setForeground(Color.red);
        }
        else if(!restric.validateSpecialC(nameJTF.getText())){
            errorJL.setVisible(true);
        }
        else{
            pets = petC.getPets(nameJTF.getText());
            
            if(pets !=null && !pets.isEmpty()){
                nameJCB.addItem("-----------");
                nameJCB.setVisible(true);
                selecJB.setVisible(true);
                for(Pet p:pets){
                    nameJCB.addItem(p.getName());
                }

            }
            else{
                nameJCB.setVisible(false);
                selecJB.setVisible(false);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void selecJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selecJBActionPerformed
        if(nameJCB.getSelectedIndex()!=0){
            pet = pets.get(nameJCB.getSelectedIndex()-1);
            nameJTF.setText("");
            System.out.println("Oye mi perro");
            nameJCB.removeAll();
            nameJCB.setVisible(false);
            selecJB.setVisible(false);
            addPanelJP.setVisible(true);
            initialize();

        }
    }//GEN-LAST:event_selecJBActionPerformed

    public void updateInfopet(){
        
    }
    private void cancelJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJBActionPerformed
        this.setVisible(false);
        nameJTF.setText("");

    }//GEN-LAST:event_cancelJBActionPerformed

    private void nameJTMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nameJTMouseClicked

        if(nameJT.getText().equals("The name field cannot be Blank")){
            nameJT.setText("");
            nameJT.setForeground(Color.black);
        }
    }//GEN-LAST:event_nameJTMouseClicked

    private void speciesJTMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_speciesJTMouseClicked

        if(speciesJT.getText().equals("The species field cannot be Blank")){
            speciesJT.setText("");
            speciesJT.setForeground(Color.black);
        }
    }//GEN-LAST:event_speciesJTMouseClicked

    private void ageJTMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ageJTMouseClicked
        if(ageJT.getText().equals("The age cannot be Blank")){
            ageJT.setText("");
            ageJT.setForeground(Color.black);
        }
    }//GEN-LAST:event_ageJTMouseClicked

    private void weithJTMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_weithJTMouseClicked
        if(weithJT.getText().equals("The weigh cannot be Blank")){
            weithJT.setText("");
            weithJT.setForeground(Color.black);
        }
    }//GEN-LAST:event_weithJTMouseClicked

    private void addJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addJBActionPerformed

        if(nameJT.getText().isEmpty()|| speciesJT.getText().isEmpty() || ageJT.getText().isEmpty()
            ||weithJT.getText().isEmpty()||
            nameJT.getText().equals("The name field cannot be Blank") ||
            speciesJT.getText().equals("The species field cannot be Blank")||
            ageJT.getText().equals("The age cannot be Blank") ||
            weithJT.getText().equals("The weigh cannot be Blank")){

            if(nameJT.getText().isEmpty()){
                nameJT.setText("The name field cannot be Blank");
                nameJT.setForeground(Color.red);
            }
            if(speciesJT.getText().isEmpty()){
                speciesJT.setText("The species field cannot be Blank");
                speciesJT.setForeground(Color.red);
            }
            if(ageJT.getText().isEmpty()){
                ageJT.setText("The age cannot be Blank");
                ageJT.setForeground(Color.red);
            }
            if(weithJT.getText().isEmpty()){
                weithJT.setText("The weigh cannot be Blank");
                weithJT.setForeground(Color.red);
            }
        }
        else{
            Pet petAux = new Pet();
            petAux.setAge(new Integer(ageJT.getText()));
            petAux.setName(nameJT.getText());
            petAux.setSpecies(speciesJT.getText());
            petAux.setWight(new Float (weithJT.getText()));
            if(petC.updatePet(pet,petAux)){
                JOptionPane.showMessageDialog(this, "The pet was update successfully");
            }
            else{
                JOptionPane.showMessageDialog(this, "error , try again");
            }
        }
    }//GEN-LAST:event_addJBActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addJB;
    private javax.swing.JPanel addPanelJP;
    private javax.swing.JLabel ageJL;
    private javax.swing.JTextField ageJT;
    private javax.swing.JButton cancelJB;
    private javax.swing.JLabel caractersJL;
    private javax.swing.JLabel errorJL;
    private javax.swing.JLabel imageF;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JComboBox nameJCB;
    private javax.swing.JLabel nameJL;
    private javax.swing.JTextField nameJT;
    private javax.swing.JTextField nameJTF;
    private javax.swing.JButton selecJB;
    private javax.swing.JLabel speciesJL;
    private javax.swing.JTextField speciesJT;
    private javax.swing.JLabel weigthJL;
    private javax.swing.JTextField weithJT;
    // End of variables declaration//GEN-END:variables

    private void initialize() {
        nameJT.setText(pet.getName());
        speciesJT.setText(pet.getSpecies());
        ageJT.setText(pet.getAge().toString());
        weithJT.setText(pet.getWight().toString()); 
        if(pet.getPicture()==null){
            imageF.setIcon(null);
        }
        else{
            BufferedImage src = null;
            try {
                src = ImageIO.read(new ByteArrayInputStream(pet.getPicture()));
            } catch (IOException ex) {
                Logger.getLogger(PetUpdateInf.class.getName()).log(Level.SEVERE, null, ex);
            }
            imageF.setIcon( rescaleImage(src, 155 , 155));                      
        }

    }
public ImageIcon rescaleImage(BufferedImage source,int maxHeight, int maxWidth)
{
    int newHeight = 0, newWidth = 0;        // Variables for the new height and width
    int priorHeight = 0, priorWidth = 0;
    BufferedImage image = source;
    ImageIcon sizeImage;

    sizeImage = new ImageIcon(image);

    if(sizeImage != null)
    {
        priorHeight = sizeImage.getIconHeight(); 
        priorWidth = sizeImage.getIconWidth();
    }

    // Calculate the correct new height and width
    if((float)priorHeight/(float)priorWidth > (float)maxHeight/(float)maxWidth)
    {
        newHeight = maxHeight;
        newWidth = (int)(((float)priorWidth/(float)priorHeight)*(float)newHeight);
    }
    else 
    {
        newWidth = maxWidth;
        newHeight = (int)(((float)priorHeight/(float)priorWidth)*(float)newWidth);
    }
    // Resize the image
    // 1. Create a new Buffered Image and Graphic2D object
    BufferedImage resizedImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = resizedImg.createGraphics();
    // 2. Use the Graphic object to draw a new image to the image in the buffer
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(image, 0, 0, newWidth, newHeight, null);
    g2.dispose();
    // 3. Convert the buffered image into an ImageIcon for return
    return (new ImageIcon(resizedImg));
}
    private void initBlankComp() {
        addPanelJP.setVisible(false);
        errorJL.setVisible(false);
        caractersJL.setVisible(false);
    }
}
